package com.wdh.kjs.Adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.activity.BankCardActivity;
import com.wdh.kjs.activity.SecuritySetting;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.util.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by CQH on 2016/4/1.
 */
public class BankcardAdapter extends BaseAdapter {
    private List<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
    private Context context;
    private String partId;
    private HashMap<String, String> maps=new HashMap<String, String>();
    public BankcardAdapter(Context context, List<HashMap<String, String>> data,String partId) {
        this.data=data;
        this.context = context;
        this.partId=partId;
    }
    private Button bind_release_cancel;
    private Button bind_release_confirm;
    private EditText bankcard_adapter_edit;
    private View view1;
    private JsonModel_my jsonModel_my;


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.bankcard_list, null);
            viewHolder.bankcard_image = (ImageView) convertView.findViewById(R.id.bankcard_image);
            viewHolder.bankcard_adapter = (TextView) convertView.findViewById(R.id.bankcard_adapter);
            viewHolder.bankcard_adapter1 = (TextView) convertView.findViewById(R.id.bankcard_adapter1);
            viewHolder.bandcard_list_alpha = (LinearLayout) convertView.findViewById(R.id.bandcard_list_alpha);
            viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);

            viewHolder.isMainCard = (TextView) convertView.findViewById(R.id.isMainCard);
            viewHolder.isMainCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    DialogShow_2("设置为主卡",position);
                    intweb_2(position);
                }
            });
            viewHolder.bind_release = (TextView) convertView.findViewById(R.id.bind_release);
            viewHolder.bind_release.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogShow("解绑银行卡",position);
                }
            });
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        maps  = data.get(position);
        viewHolder.bankcard_adapter.setText(maps.get("bank"));
        viewHolder.bankcard_adapter1.setText(maps.get("bankcard_number"));
        switch (maps.get("bank")){
            case "中国农业银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_nongye);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_nongye);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "中国工商银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_gongshang);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_gongshang);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "中国建设银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_jianshe);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_jianshe);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "民生银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_minsheng);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_minsheng);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "兴业银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_xingye);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_xingye);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "交通银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_jiaotong);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_jiaotong);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "中信银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_zhongxin);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_zhongxin);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "平安银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_pingan);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_pingan);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "广发银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_guangfa);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_guangfa);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "招商银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_zhaoshang);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_zhaoshang);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "中国银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_zhongguo);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_gongshang);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "浦发银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_pufa);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bandcard_pufa);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "福建海峡银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_fujianhaixia);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_haixia);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "光大银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_guangda);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_guangda);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "恒丰银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_huifeng);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_hengfeng);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "华夏银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_huaxia);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_huaxia);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "汇丰银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_huifeng);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_huifeng);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "泉州银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_quanzhou);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_quanzhou);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "厦门银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_xiamen);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_xiamen);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "厦门国际银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_xiamenguoji);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_xiamenguoji);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "上海银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_shanghai);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_shanghai);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "香港恒生银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_xiangganghengsheng);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_xiangganghengfeng);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "邮政银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_youzheng);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_youzheng);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            case "浙商银行":
                viewHolder.bankcard_image.setBackgroundResource(R.drawable.bc_zheshang);
                viewHolder.bandcard_list_alpha.setBackgroundResource(R.drawable.bg_zheshang);
                viewHolder.bandcard_list_alpha.getBackground().setAlpha(30);
                break;
            default:
                break;
        }

//        viewHolder.isMainCard.setText(maps.get("isMainCard"));
        if(Integer.parseInt(maps.get("isMainCard"))==1){
            viewHolder.isMainCard.setText("默认为主卡");
            viewHolder.isMainCard.setTextColor(Color.parseColor("#d90000"));
            viewHolder.isMainCard.setBackgroundResource(R.drawable.bt_style_yellow);
        }else {
            viewHolder.isMainCard.setText("设为主卡");
            viewHolder.isMainCard.setTextColor(Color.parseColor("#FFFFFF"));
            viewHolder.isMainCard.setBackgroundResource(R.drawable.bt_transparent_stroke);
        }
        return convertView;
    }

    private void intweb(final int position) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("partyId", partId);
        params.addBodyParameter("bankCardId", data.get(position).get("bankcardId"));
        params.addBodyParameter("tradePwd",bankcard_adapter_edit.getText().toString());
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_BANKCARD_DELETE, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
//                Log.i("msg",responseInfo.result+"      555555555555555");
                jsonModel_my = gson.fromJson(responseInfo.result, JsonModel_my.class);
                if (jsonModel_my.getMessageEncode().equals("MG013103")) {
                        trueDialogShow("解绑银行卡成功！");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(context,BankCardActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                context.startActivity(intent);
                            }
                        }, 2000);
                }else if(jsonModel_my.getMessageEncode().equals("MG012201")) {
                    falseDialogShow("交易密码错误！");
                }else if(jsonModel_my.getMessageEncode().equals("MG013106")){
                    TipDialogShow("不能解绑主卡！");
                }
            }
            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(context, "网络异常！");
            }
        });

    }

    private void intweb_2(final int position) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("partyId", BankCardActivity.partyId);
        params.addBodyParameter("bankCardId", data.get(position).get("bankcardId"));
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_BANKCARD_DEFAULT, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                jsonModel_my = gson.fromJson(responseInfo.result, JsonModel_my.class);
                if (jsonModel_my.getMessageEncode().equals("MG013107")) {
                    trueDialogShow("设置为主卡成功！");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(context,BankCardActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            context.startActivity(intent);
                        }
                    }, 2000);
                }else
                    ToastUtils.showShort(context,"设置为主卡失败");
            }
            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(context, "网络异常！");
            }
        });

    }

    private void DialogShow(String hint,final int num) {
        View dialog= View.inflate(context,R.layout.bind_release_2,null);
        Button bt_dialog_false_negative = (Button) dialog.findViewById(R.id.bind_release_cancel);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bind_release_confirm);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.bind_release_title);
        bankcard_adapter_edit = (EditText) dialog.findViewById(R.id.bankcard_adapter_edit);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_negative.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
        bt_dialog_false_positive.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                dialogShow.dismiss();
                if(bankcard_adapter_edit.getText().toString().equals("")) {
                    falseDialogShow("请输入交易密码！");
                }else {
                    Log.i("msg","银行卡长度  "+maps.get("bandCard_size"));

                    if(Integer.parseInt(maps.get("bandCard_size"))==1){
                        TipDialogShow("必须至少保留一张银行卡!");
                    }else {
                        intweb(num);
                    }

                }
            }
        });
    }

    private void falseDialogShow(String hint) {
        View dialog= View.inflate(context,R.layout.view_dialog_false,null);
        Button bt_dialog_false_negative = (Button) dialog.findViewById(R.id.bt_dialog_false_negative);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bt_dialog_false_positive);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_false_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_negative.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
        bt_dialog_false_positive.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
                bankcard_adapter_edit.setText("");

            }
        });
    }

    private void trueDialogShow(String hint) {
        View dialog= View.inflate(context,R.layout.view_dialog_true,null);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_true_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();

    }

    private void TipDialogShow(String hint) {
        View dialog= View.inflate(context,R.layout.view_dialog_tip,null);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bt_dialog_false_positive);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_false_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_positive.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
    }

    class ViewHolder{
        ImageView bankcard_image;
        TextView bankcard_adapter;
        TextView bankcard_adapter1;
        TextView bind_release;
        TextView isMainCard;
        LinearLayout bandcard_list_alpha;
        LinearLayout ii_bankcard_list;
    }
}
